package com.zzx.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xml.internal.resolver.CatalogException;
import com.zzx.boot.common.CustomException;
import com.zzx.boot.common.R;
import com.zzx.boot.dto.SetmealDto;
import com.zzx.boot.pojo.*;
import com.zzx.boot.service.CategoryService;
import com.zzx.boot.service.DishService;
import com.zzx.boot.service.SetmealDishService;
import com.zzx.boot.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@ResponseBody
@RequestMapping("/setmeal")
public class SetmealController {
    @Resource
    private SetmealService setmealService;
    @Resource
    private SetmealDishService setmealDishService;

    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        R<String> stringR = setmealService.saveSetmeal(setmealDto);
        return stringR;
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> pages(Integer page,Integer pageSize,String name){
        R<Page<SetmealDto>> pageR = setmealService.pageAndSetmeal(page, pageSize, name);
        return pageR;
    }

    @GetMapping("/{id}")
    public R<SetmealDto> show(@PathVariable Long id){
        R<SetmealDto> setmealDtoR = setmealService.updateAndById(id);
        return setmealDtoR;
    }

    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        R<String> stringR = setmealService.saveUpdate(setmealDto);
        return stringR;
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("status/{status}")
        public R<String> status(@PathVariable Integer status,String ids){
        String[] split = ids.split(",");
        List<Setmeal> list = new ArrayList<>();
        for (String s : split) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(Long.valueOf(s));
            list.add(setmeal);
        }
//        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper();
//        updateWrapper.set(Setmeal::getStatus,status);
//        updateWrapper.eq(Setmeal::getId,ids);
//        boolean update = setmealService.update(updateWrapper);
        boolean update = setmealService.updateBatchById(list);
        if (!update){
            throw new CustomException("修改失败!");
        }
        return R.success("修改成功");
    }

    //将类别名称为setmealCache 的所有缓存都清除掉
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> deletes(String ids){
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        boolean b = setmealService.removeByIds(strings);
        if (!b){
            throw new CustomException("删除失败!");
        }
        return R.success("删除成功");
    }

    //如果缓存中有数据则直接返回缓存中的数据
    //如果没有则执行方法在将方法的返回值存储到缓存中
    @Cacheable(value = "setmealCache",key = "'setmeal_'+#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId()).eq(Setmeal::getStatus,setmeal.getStatus());
        //查询到套餐的菜品了
        List<Setmeal> list = setmealService.list(queryWrapper);
        if (list.size()==0){
            return R.error("当前分类暂无套餐");
        }
        return R.success(list);
    }

}
